/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Admin;

import Server.Database.Database;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;

/**
 *
 * @author sarunaskujalis
 */
public class AdminWindowBlocks extends javax.swing.JPanel {
    
    private Database db;
    private DefaultListModel listModel;
    private static AdminWindowBlocks instance;
    
    /**
     * Creates new form AdminWindowBlocks
     */
    public AdminWindowBlocks(Database db) {
        instance = this;
        this.db = db;
        initComponents();
        listModel = new DefaultListModel();
        blocksList.setModel(listModel);
        loadList();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        blocksList = new javax.swing.JList();
        unblockUserBtn = new javax.swing.JButton();

        blocksList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                handleListSelection(evt);
            }
        });
        jScrollPane1.setViewportView(blocksList);

        unblockUserBtn.setText("Atbl. darbuotoją");
        unblockUserBtn.setEnabled(false);
        unblockUserBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                unblockUserBtnActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(unblockUserBtn)
                .add(15, 15, 15))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(unblockUserBtn)
                        .add(0, 0, Short.MAX_VALUE))
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void unblockUserBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_unblockUserBtnActionPerformed
        // TODO add your handling code here:
        String selection = blocksList.getSelectedValue().toString();
        int userIndex = blocksList.getSelectedIndex();
        int lastIndex = selection.indexOf(":")-1;
        selection = selection.substring(0, lastIndex);
        try {
            db.usersRegister.unblockUser(selection);
            listModel.remove(userIndex);
        } catch (SQLException ex) {
            Logger.getLogger(AdminWindowBlocks.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
    }//GEN-LAST:event_unblockUserBtnActionPerformed

    private void handleListSelection(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_handleListSelection
        // TODO add your handling code here:
        if (!blocksList.isSelectionEmpty()) {
            unblockUserBtn.setEnabled(true);
        }
    }//GEN-LAST:event_handleListSelection
    
    private void loadList() {
        try {
            ArrayList<String> list = db.usersRegister.getBlockedUsersAndReasons();
            for (String user : list) {
                listModel.addElement(user);
            }
        } catch (SQLException ex) {
            Logger.getLogger(AdminWindowBlocks.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void addToList(String name, String reason) {
        listModel.addElement(name+" : "+reason);
    }
    
    public static AdminWindowBlocks getInstance() {
        return instance;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList blocksList;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton unblockUserBtn;
    // End of variables declaration//GEN-END:variables
}
